package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.dto.NursingPlanDTO;
import com.zzyl.nursing.domain.vo.NursingPlanAllVO;
import com.zzyl.nursing.domain.vo.NursingPlanVO;
import com.zzyl.nursing.domain.vo.NursingProjectPlanVO;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.service.INursingProjectPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author lhb
 * @date 2025-09-12
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVO selectNursingPlanById(Long id) {
        NursingPlanVO vo = new NursingPlanVO();
        NursingPlan plan = this.getById(id);
        if (plan == null) {
            throw new RuntimeException("数据不存在!");
        }
        BeanUtils.copyProperties(plan, vo);
        vo.setId(id.toString());
        List<NursingProjectPlanVO> nursingProjectPlanVOS = nursingPlanMapper.getProjectPlansByPlanId(id);
        vo.setProjectPlans(nursingProjectPlanVOS);
        System.out.println("=========vo=========" + vo);
        //查询项目计划表
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        this.save(nursingPlan);
        if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
            //给他添加plan_id
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            projectPlans.forEach(p -> p.setPlanId(nursingPlan.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        //如果执行到这里说明没有错误
        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO dto) {
        //先更新nursing_plan
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(dto, plan);
        this.updateById(plan);
        //然后删除nursing_project_plan表的中数据
        LambdaQueryWrapper<NursingProjectPlan> lqw = new LambdaQueryWrapper<>();
        lqw.eq(NursingProjectPlan::getPlanId, plan.getId());
        nursingProjectPlanService.remove(lqw);
        //如果不能null,然后就新增表中的数据
        if (dto.getProjectPlans() != null && dto.getProjectPlans().size() > 0) {
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            projectPlans.forEach(pp -> pp.setPlanId(plan.getId()));
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Integer id) {
        //先查询有没有护理等级使用护理计划
        LambdaQueryWrapper<NursingLevel> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(NursingLevel::getLplanId, id);
        NursingLevel nursingLevel = nursingLevelMapper.selectOne(lqw1);
        if (nursingLevel != null) {
            throw new RuntimeException("护理计划关联了护理等级,不能删除");
        }

        //删除计划
        this.removeById(id);
        //删除对应关系表中的数据
        LambdaQueryWrapper<NursingProjectPlan> lqw = new LambdaQueryWrapper<>();
        lqw.eq(NursingProjectPlan::getPlanId, id);
        nursingProjectPlanService.remove(lqw);
        return 1;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public List<NursingPlanAllVO> getAll() {
        return nursingPlanMapper.getAll();
    }
}
